/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface;

import Controladores.MedicalrecordJpaController;
import Controladores.PetJpaController;
import Controladores.exceptions.PreexistingEntityException;
import entidades.Medicalrecord;
import entidades.Pet;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JOptionPane;

/**
 *
 * @author Gatoper
 */
public class JPIMedicalRecord extends javax.swing.JPanel {

    /**
     * Creates new form JPIMedicalRecord
     */
    public JPIMedicalRecord() {
        initComponents();
        this.setSize(new Dimension(300, 400));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LID = new javax.swing.JLabel();
        Lnamepet = new javax.swing.JLabel();
        TID = new javax.swing.JTextField();
        CBPetname = new javax.swing.JComboBox();
        Bcrear = new javax.swing.JButton();

        LID.setText("ID:");

        Lnamepet.setText("Pet");

        EntityManagerFactory emf;
        EntityManager em;
        Map<String, String> properties=new HashMap<>();
        properties.put("javax.persistence.jdbc:user", "root");
        properties.put("javax.persistence.jdbc:password", "");
        emf=Persistence.createEntityManagerFactory("HPVAS",properties);
        em=emf.createEntityManager();
        Pet pet=new Pet();
        PetJpaController pjc=new PetJpaController(emf);
        List l=pjc.findPetEntities();
        String [] aux=new String[l.size()];
        for(int i=0;i<l.size();i++)
        aux[i]=((Pet)l.get(i)).getName()+"";
        CBPetname.setModel(new javax.swing.DefaultComboBoxModel(aux));

        Bcrear.setText("Create");
        Bcrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BcrearActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(LID)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(TID, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(Lnamepet)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(CBPetname, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(Bcrear))
                .addContainerGap(20, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LID)
                    .addComponent(TID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Lnamepet)
                    .addComponent(CBPetname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(Bcrear)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BcrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BcrearActionPerformed
        // TODO add your handling code here:
        boolean flag=true;
        if(TID.getText().length()<1) flag=false;
        if(flag){
            EntityManagerFactory emf;
            EntityManager em;
            Map<String, String> properties = new HashMap<>();
            emf = Persistence.createEntityManagerFactory("HPVAS");
            em = emf.createEntityManager();
            PetJpaController pjc=new PetJpaController(emf);
            Pet pet=pjc.findPet(this.CBPetname.getSelectedItem().toString());
            Medicalrecord medicalrecord=new Medicalrecord(pet, Long.parseLong(this.TID.getText()));
            MedicalrecordJpaController  mjc=new MedicalrecordJpaController(emf);
            try {
                mjc.create(medicalrecord);
                JOptionPane.showMessageDialog(this, "Exito");
            } catch (PreexistingEntityException ex) {
                Logger.getLogger(JPIMedicalRecord.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception ex) {
                Logger.getLogger(JPIMedicalRecord.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else{
            JOptionPane.showMessageDialog(this, "Por favor llene todos los campos");
        }
        
    }//GEN-LAST:event_BcrearActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Bcrear;
    private javax.swing.JComboBox CBPetname;
    private javax.swing.JLabel LID;
    private javax.swing.JLabel Lnamepet;
    private javax.swing.JTextField TID;
    // End of variables declaration//GEN-END:variables
}
